using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using System.Text;
using System.IO;
using Microsoft.SPOT.Hardware;

namespace SDCardWriteTests
{
    public class Sensors
    {
        #region Acceleration Inputs
        AnalogIn x18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An0);
        AnalogIn y18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An1);
        AnalogIn x5G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An2);
        AnalogIn y5G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
        AnalogIn z250G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An4);
        AnalogIn z18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An5);
        PWM pulse = new PWM((PWM.Pin)FEZ_Pin.PWM.Di8);
        InterruptPort i = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di7, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
        byte[] data = new byte[12+8];
        #endregion

        public string _filepath = "test.csv";
        FileStream fs;
        DateTime start = DateTime.Now;
        public Sensors(string rootpath)
        {
            _filepath = Path.Combine(rootpath, _filepath);
            fs = new FileStream(_filepath, FileMode.OpenOrCreate);
            
        }
        public void HighSpeedRead()
        {
            //byte[] data = new byte[12];
            for (int i = 0; i < 1600; i++)
            {
                var ticks = DateTime.Now.Ticks;
                var x18=x18G.Read();                 
                data[0] = (byte)(x18 << 8);   data[1] = (byte)(x18);

                var y18 = y18G.Read();
                data[2] = (byte)(y18 << 8); data[3] = (byte)(y18);
                
                var x5 = x5G.Read();
                data[4] = (byte)(x5 << 8); data[5] = (byte)(x5);
                
                var y5 = y5G.Read();
                data[6] = (byte)(y5 << 8); data[7] = (byte)(y5);
                
                var z250 = z250G.Read();
                data[8] = (byte)(z250 << 8); data[9] = (byte)(z250);
                
                var z18 = z18G.Read();
                data[10] = (byte)(z18 << 8); data[11] = (byte)(z18);

                var l = DateTime.Now.Ticks;
                data[12] = (byte)l;
                data[13] = (byte)(l>>8);
                data[14] = (byte)(l >> 16);
                data[15] = (byte)(l >> 24);
                data[16] = (byte)(l>>32);
                data[17] = (byte)(l >> 40);
                data[18] = (byte)(l >> 48);
                data[19] = (byte)(l >> 56);
                                
                fs.Write(data,0,15);
            }
            fs.Flush();
        }
    }
}
